package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.AnnualVeriForm;

public class AnnualVeriDAO extends BaseHibernateDAO {
	public AnnualVeriDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(AnnualVeriDAO.class);
	
	public List findAllPage(AnnualVeriForm aForm, String id) {
		try {
			String queryString = "from AnnualVeri where annualVeriDelOper='0' and employerInfo = '"+id+"'";
			if(aForm.getAnnualVeriYear() != null && !"".equals(aForm.getAnnualVeriYear())){
				queryString += " and annualVeriYear ='" + aForm.getAnnualVeriYear() + "'";
			}
			queryString += " order by annualVeriYear desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(9);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	

	
	public List findByYear(String y, String unitId) {
		log.debug("getting AnnualVeri instance with id: " + y);
		try {
			String queryString = "from AnnualVeri where annualVeriYear= '" + y + "' and employerInfo = '" + unitId + "'";
			Query queryObject = getSession().createQuery(queryString);
//			queryObject.setParameter(0, y);
//			queryObject.setParameter(1, unitId);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public void save(AnnualVeri detachedInstance) {
		log.debug("saving AnnualVeri instance");
		try {
			getSession().save(detachedInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	
	
	
	public AnnualVeri findAnnuVeriByUnitId(java.lang.String id) {
		log.debug("getting AnnualVeri instance with id: " + id);
		try {
			String queryString = "from AnnualVeri where annualVeriUnitId=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setString(0, id);
			List<AnnualVeri> list = queryObject.list();
			if(list != null && list.size() > 0){
				return (AnnualVeri)list.get(0);
			}else
				return null;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
	public List findAllPage(String annualVeriYear, String annualVeriState, String empInfoName) {
		try {
			String queryString = "from AnnualVeri where annualVeriDelOper='0'";
			if(annualVeriYear != null && !"".equals(annualVeriYear)){
				queryString += " and annualVeriYear ='" + annualVeriYear + "'";
			}
			if(annualVeriState != null && !"".equals(annualVeriState)){
				queryString += " and annualVeriState ='" + annualVeriState + "'";
			}
			if(empInfoName != null && !"".equals(empInfoName)){
				queryString += " and employerInfo.empInfoName like '%" + empInfoName + "%'";
			}
			//queryString += " order by annualVeriState Asc, annualVeriYear desc";
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			if(queryObject !=null && list.size()>0){
			  this.setPageSize(8);
			  list = this.jlist(queryObject);
			}
			return list;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public AnnualVeri findById(java.lang.String id) {
		log.debug("getting AnnualVeri instance with id: " + id);
		try {
			AnnualVeri instance = (AnnualVeri) getSession().get(
					"com.ldj.hibernate.AnnualVeri", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public void merge(AnnualVeri detachedInstance) {
		log.debug("merging AnnualVeri instance");
		try {
			getSession().merge(detachedInstance);
			log.debug("merge successful");
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}